programming4us
           
 
 
Windows

Windows 7 : Working at the Command Line (part 3)

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
12/5/2010 11:47:09 AM

Redirecting Command Output and Input

Windows 7 is always directing things here and there. This generally falls into two categories:

  • Directing data into its commands from a device called standard input

  • Directing data out of its commands to a device called standard output

A device called CON (console) normally handles standard input and standard output, which is your keyboard and monitor. Windows 7 assumes that all command input comes from the keyboard and that all command output (such as a DIR listing or a system message) goes to the screen. Redirection is just a way of specifying different input and output devices.

Redirecting Command Output

To send command output to somewhere other than the screen, you use the output redirection operator (>). One of the most common uses for output redirection is to capture the results of a command in a text file. For example, you might want to use the report produced by the SYSTEMINFO command as part of a word processing document. You could use the following command to first capture the report as the file systeminfo.csv:

systeminfo /fo csv > %userprofile%\systeminfo.csv

When you run this command, the usual SYSTEMINFO data doesn’t appear onscreen. That’s because you directed it away from the screen and into the systeminfo.csv file.

You can use this technique to capture DIR listings, CHKDSK reports, and more. One caveat: If the file you specify as the output destination already exists, Windows 7 overwrites it without warning. To avoid this, you can use the double output redirection symbol (>>). This tells Windows 7 to append the output to the end of the file if the file exists. For example, suppose you used the following command to output the results of the CHKDSK C: command to chkdsk.txt:

chkdsk c: > %userprofile%\chkdsk.txt

If you then want to append the results of the CHKDSK D: command to chkdsk.txt, you enter the following command:

chkdsk d: >> %userprofile%\chkdsk.txt

You can also redirect output to different devices. Table 4 lists the various devices that Windows 7 installs each time you start your system.

Table 4. Devices Installed by Windows 7 When You Start Your System
Device NameDevice
AUXAuxiliary device (usually COM1)
CLOCK$Real-time clock
COMnSerial port (COM1, COM2, COM3, or COM4)
CONConsole (keyboard and screen)
LPTnParallel port (LPT1, LPT2, or LPT3)
NULNUL device (nothing)
PRNPrinter (usually LPT1)

For example, you can send a DIR listing to the printer with the following command. (Of course, you need to be sure that your printer is on before doing this. Also note that this only works for a printer attached to a parallel port; it doesn’t work for USB printers.)

dir > prn

The NUL device usually throws people for a loop when they first see it. This device (affectionately known as the bit bucket) is, literally, nothing. Batch files normally use it to suppress the usual messages Windows 7 displays when it completes a command. For example, Windows 7 normally says 1 file(s) copied when you copy a file. However, the following command sends that message to NUL, so you wouldn’t see it onscreen:

copy somefile.doc \\server\users\paul\ > nul

Tip

Unfortunately, Windows 7 gives you no way to redirect output to a USB port. However, there’s a workaround you can use if you’re trying to redirect output to a USB printer. Assuming the printer is shared and that no other device is using the port LPT2, run the following command:

NET USE LPT2 \\server\printer

Here, replace server with the name of your Windows 7 computer and printer with the share name of the USB printer. Now, when you redirect output to LPT2, Windows 7 sends the output to the USB printer.


Redirecting Input

The input redirection operator (<) handles getting input to a Windows 7 command from somewhere other than the keyboard. Input redirection is almost always used to send the contents of a text file to a Windows 7 command. The most common example is the MORE command, which displays one screen of information at a time. If you have a large text file that scrolls off the screen when you use TYPE, the following command, which sends the contents of BIGFILE.TXT to the MORE command, solves the problem:

more < bigfile.txt

When you run this command, the first screen of text appears, and the following line shows up at the bottom of the screen:

-- More --

Just press any key, and MORE displays the next screen. (Whatever you do, don’t mix up < and > when using MORE. The command more > bigfile.txt erases BIGFILE.TXT!) MORE is an example of a filter command. Filters process whatever text is sent through them. The other Windows 7 filters are SORT and FIND, which I discuss in a moment.

Another handy use for input redirection is to send keystrokes to Windows 7 commands. For example, create a text file called enter.txt that consists of a single press of the Enter key, and then try this command:

date < enter.txt

Windows 7 displays the current date, and instead of waiting for you to either type in a new date or press Enter, it just reads enter.txt and uses its single carriage return as input. (For an even easier way to input the Enter key to a command, check out the next section.)

One common recipient of redirected input is the SORT command. SORT, as you might guess from its name, sorts the data sent to it and displays the results onscreen. So, for example, here’s how you would sort a file called JUMBLED.TXT:

sort < jumbled.txt

Instead of merely displaying the results of the sort onscreen, you can use > to redirect them to another file.

Tip

SORT normally starts with the first column and works across. To start with any other column, use the /+n switch, where n is the number of the column you want to use. To sort a file in reverse order (that is, a descending sort—Z to A, then 9 to 0—instead of an ascending sort—0 to 9, then A to Z), use the /R switch.


Piping Commands

Piping is a technique that combines both input and output redirection. Using the pipe operator (|), the output of one command is captured and sent as input to another command. For example, the SYSTEMINFO command displays about five screens of data, so you usually need to scroll back to see the data you’re looking for. However, you can pause the output by piping it to the MORE command:

systeminfo | more

The pipe operator captures the SYSTEMINFO output and sends it as input to MORE, which then displays the SYSTEMINFO results one screen at a time.

Note

Piping works by first redirecting the output of a command to a temporary file. It then takes this temporary file and redirects it as input to the second command. A command such as SYSTEMINFO | MORE is approximately equivalent to the following two commands:

SYSTEMINFO > tempfile

MORE tempfile


I showed you in the preceding section how to use input redirection to send keystrokes to a Windows 7 command. But if you have to send only a single key, piping offers a much nicer solution. The secret is to use the ECHO command to echo the character you need and then pipe it to the Windows 7 command.

For example, if you use the command DEL *.*, Windows 7 always asks whether you’re sure that you want to delete all the files in the current directory. This is a sensible precaution, but you can override it if you do things this way:

echo y | del *.*

Here, the y that would normally be echoed to the screen is sent to DEL instead, which interprets it as a response to its prompt. This is a handy technique for batch files in which you want to reduce or even eliminate user interaction.

Tip

You can even use this technique to send an Enter keypress to a command. The command ECHO. (that’s ECHO followed by a period) is equivalent to pressing Enter. So, for example, you could use the following command in a batch file to display the time without user input:

ECHO. | TIME
Other -----------------
- Windows 7 : Getting to the Command Line (part 2) - Running CMD
- Windows 7 : Getting to the Command Line (part 1)
- Windows Azure : Programming Access Control Service (part 10) - Deploying the Web Service in Windows Azure
- Windows Azure : Programming Access Control Service (part 9) - Configuring a Web Service Client to Acquire and Send SAML Tokens
- Windows Azure : Programming Access Control Service (part 8)
- Windows Azure : Programming Access Control Service (part 7) - Integrating ACS with a SAML Token Provider
- Windows Azure : Programming Access Control Service (part 6)
- Windows Azure : Programming Access Control Service (part 5)
- Windows Azure : Programming Access Control Service (part 4)
- Windows Azure : Programming Access Control Service (part 3)
- Windows Azure : Programming Access Control Service (part 2)
- Windows Azure : Programming Access Control Service (part 1)
- Windows 7 : Working with Registry Entries (part 3)
- Windows 7 : Working with Registry Entries (part 2)
- Windows 7 : Working with Registry Entries (part 1) - Changing the Value of a Registry Entry
- Windows 7 : Keeping the Registry Safe
- Windows 7 : Getting to Know the Registry (part 2)
- Windows 7 : Getting to Know the Registry (part 1) - Understanding Registry Settings
- Windows 7 : Firing Up the Registry Editor
- Windows Azure : Managing Access Control Service Resources (part 2)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us